<?php
/**
 * Created by PhpStorm.
 * Created at 18/05/2018 10:29.
 */

require_once __DIR__ . '/../vendor/autoload.php';

$container = new \Illuminate\Container\Container();
// dump($container);

// 每次通过Container::make获取的都是新对象 new instance
$container->bind('foo', \App\Foo::class);
// dump($container->make('foo') === $container->make('foo'));  // false

// 注册单例，不言而喻，每次通过Container::make获取的是同一个对象
$container->singleton('sharedFoo', \App\Foo::class);
// dump($container->make('sharedFoo') === $container->make('sharedFoo'));  // true

// 注册的是一个实例，而不是类名, 每次通过Container::make获取的都是该实例
$container->instance('instancedFoo', new \App\Foo());
// dump($container->make('instancedFoo') === $container->make('instancedFoo'));  // true


// 绑定一个回调，当指定type的rebind事件被触发时会调用
// rebind事件什么时候会被触发：已经存在的type被重新绑定了，且此前已经被实例化过
$container->rebinding('foo', function ($container, $abstractInstance) {
    dump('The foo was rebound.');
});

$container->make('foo');
$container->bind('foo', function () {
    dump('The foo was made.');
});

// 可以像「访问数组」一样对容器进行访问，因为它实现了PHP的 ArrayAccess 接口
// dump($container['sharedFoo']);

// dump($container);
